package com.todo.content.application.frontend.service.impl;

import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.content.application.base.apollo.ContentApollo;
import com.todo.content.application.convert.ContentConvert;
import com.todo.content.application.entity.bo.ContentClassifyBO;
import com.todo.content.application.entity.condition.ContentClassifyConditionBO;
import com.todo.content.application.frontend.service.ContentClassifyAggService;
import com.todo.content.application.service.ContentClassifyService;
import com.todo.content.iface.frontend.request.ContentClassifyBaseListParam;
import com.todo.content.iface.frontend.request.QueryContentClassifyDetailByCodeParam;
import com.todo.content.iface.frontend.request.RecommendContentClassifyListParam;
import com.todo.content.iface.frontend.response.ContentClassifyBaseVO;
import com.todo.content.iface.frontend.response.ContentClassifyDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ContentClassify)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:45
 */
@Slf4j
@Service("contentClassifyAggService")
public class ContentClassifyAggServiceImpl implements ContentClassifyAggService {

    @Resource
    private ContentClassifyService contentClassifyService;
    @Resource
    private ContentApollo contentApollo;

    @Override
    public BizPageResponse<ContentClassifyBaseVO> queryContentClassifyList(ContentClassifyBaseListParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        //TODO 此处设置二级缓存分别是30min/24h
        ContentClassifyConditionBO classifyConditionBO = ContentConvert.convertToContentClassifyConditionBO(param);
        PageResult<ContentClassifyBO> contentClassifyBOPageResult = contentClassifyService.queryContentClassifyByPage(classifyConditionBO);
        if (contentClassifyBOPageResult == null) {
            return BizPageResponse.success();
        }

        return BizPageResponse.success(ContentConvert.convertToPageContentClassifyBaseVO(contentClassifyBOPageResult));
    }

    @Override
    public BizResponse<List<ContentClassifyBaseVO>> baseRecommendContentClassifyList(RecommendContentClassifyListParam param) {
        if (param == null || !param.baseCheck()) {
            return BizResponse.success();
        }
        //TODO 此处设置二级缓存分别是30min/24h 注：apollo配置做修改回调，调整后刷新缓存
        List<Long> classifyIdList = contentApollo.queryRecommendContentClassifyList();
        if (CollectionUtils.isEmpty(classifyIdList)) {
            return BizResponse.success();
        }
        List<ContentClassifyBO> contentClassifyBOS = contentClassifyService.queryContentClassifyByIdList(classifyIdList);
        return BizResponse.success(ContentConvert.convertToContentClassifyBaseVOList(contentClassifyBOS));
    }

    @Override
    public BizResponse<ContentClassifyDetailVO> queryContentClassifyDetailByCode(QueryContentClassifyDetailByCodeParam param) {
        if (param == null || !param.baseCheck()) {
            return BizResponse.success();
        }
        ContentClassifyBO contentClassifyBO = contentClassifyService.queryContentClassifyByCode(param.getClassifyCode());
        return BizResponse.success(ContentConvert.convertToContentClassifyDetailVO(contentClassifyBO));
    }
}
